<?php
/**
 * Created by PhpStorm.
 * User: Merdan
 * Date: 2017/3/28
 * Time: 14:37
 */

namespace App\Http\Controllers;


use App\Exceptions\BaseException;
use App\Http\Requests\ProductRequest;
use App\Services\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    protected $productService;

    function __construct()
    {
        parent::__construct();
        $this->productService = new Product();
    }

    /**
     * 物料列表
     *
     * @param Request $request
     * @return array
     */
    public function index(Request $request)
    {
        try{
            $limit = intval($request->get('count',12));
            $filter = $request->get('filter');
            $products = $this->productService->getList($limit, $filter);
            return $this->success('',$products);
        }catch(BaseException $e){
            return $this->fail($e->getMessage());
        }catch (\Exception $e){
            \Log::error($e);
            return $this->fail('发生意外错误, 请稍后重试!');
        }
    }

    /**
     * 获取单个物料信息
     *
     * @param $id
     * @return array
     */
    public function show($id)
    {
        try{
            $id = intval($id);
            $product = $this->productService->show($id);
            return $this->success('', $product);
        }catch(BaseException $e){
            return $this->fail($e->getMessage());
        }catch (\Exception $e){
            \Log::error($e);
            return $this->fail('发生意外错误, 请稍后重试!');
        }
    }

    /**
     * 新增物料
     *
     * @param ProductRequest $request
     * @return array
     */
    public function store(ProductRequest $request)
    {
        try{
            $name = trim($request->get('name', ''));
            $category_id = trim($request->get('category_id', 0));
            $unit = trim($request->get('unit', '无'));
            $notes = trim($request->get('notes', ''));
            $this->productService->create(compact('name', 'category_id', 'unit', 'notes'));
            return $this->success('新增物料成功!');

        }catch(BaseException $e){
            return $this->fail($e->getMessage());
        }catch (\Exception $e){
            \Log::error($e);
            return $this->fail('发生意外错误, 请稍后重试!');
        }
    }

    /**
     * 更新物料信息
     *
     * @param ProductRequest $request
     * @param $id
     * @return array
     */
    public function update(ProductRequest $request, $id)
    {
        try{
            $id = intval($id);
            $name = trim($request->get('name', ''));
            $category_id = trim($request->get('category_id', 0));
            $unit = trim($request->get('unit', '无'));
            $notes = trim($request->get('notes', ''));
            $this->productService->update($id, compact('name', 'category_id', 'unit', 'notes'));
            return $this->success('物料已更新!');
        }catch(BaseException $e){
            return $this->fail($e->getMessage());
        }catch (\Exception $e){
            \Log::error($e);
            return $this->fail('发生意外错误, 请稍后重试!');
        }
    }

    /**
     * 反悔所有分类
     *
     * @return array
     */
    public function categories()
    {
        try{
            $products = $this->productService->cateogries();
            return $this->success('', $products);
        }catch(BaseException $e){
            return $this->fail($e->getMessage());
        }catch (\Exception $e){
            \Log::error($e);
            return $this->fail('发生意外错误, 请稍后重试!');
        }
    }
}